package com.ossjk.oa.advisory.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.oa.advisory.entity.Counterpart;
import com.ossjk.oa.advisory.entity.Counterpartfw;
import com.ossjk.oa.advisory.entity.Customer;
import com.ossjk.oa.advisory.entity.School;
import com.ossjk.oa.advisory.service.ICounterpartService;
import com.ossjk.oa.advisory.service.ICounterpartfwService;
import com.ossjk.oa.advisory.service.ISchoolService;
import com.ossjk.oa.system.entity.Employee;
import com.ossjk.oa.system.service.IEmployeeService;

/**
 * <p>
 * 学校对接人管理 前端控制器
 * </p>
 *
 * @author Jason
 * @since 2019-01-14
 */
@RestController
@RequestMapping("/advisory/counterpart")
public class CounterpartController extends BaseController {
	@Autowired
	private ICounterpartService iCounterpartService;
	@Autowired
	private ICounterpartfwService iCounterpartfwService;
	@Autowired
	private ISchoolService iSchoolService;
	@Autowired
	private IEmployeeService iEmployeeService;

	@RequiresPermissions("00150010")
	@GetMapping("/list")
	public Object list(String name, String sid, String stime, String etime, Page page) {
		Wrapper wrapper = Condition.create().isWhere(true).ne("c.isdelete", 2);
		if (!CommonUtil.isBlank(name)) {
			wrapper.like("c.name", name);
		}
		if (!CommonUtil.isBlank(sid)) {
			wrapper.eq("c.sid", sid);
		}
		if (!CommonUtil.isBlank(stime)) {
			wrapper.ge("cfw.mtime", stime);
		}
		if (!CommonUtil.isBlank(etime)) {
			wrapper.le("cfw.mtime", etime);
		}
		wrapper.orderBy("cfw.mtime", false).orderBy("crtm", false);
		page = iCounterpartService.selectVoPage(page, wrapper);
		return responseSuccess(page);
	}

	@RequiresPermissions("001500100020")
	@GetMapping("/getRecords")
	public Object getRecords(String cid, Page page) {
		Wrapper wrapper = Condition.create().isWhere(true).ne("cfw.isdelete", 2).orderBy("crtm");
		if (!CommonUtil.isBlank(cid)) {
			wrapper.eq("cfw.cid", cid);
		}
		page = iCounterpartfwService.selectVoPage(page, wrapper);
		return responseSuccess(page);
	}

	@RequiresPermissions("001500100000")
	@PostMapping("/insert")
	public Object insert(Counterpart counterpart) {

		if (iCounterpartService.insert(counterpart)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("001500100025")
	@GetMapping("/toInsertRecords/{id}")
	public Object toInsertRecords(@PathVariable("id") String id) {

		return responseSuccess(iCounterpartService.selectById(id));
	}

	@RequiresPermissions("001500100025")
	@PostMapping("/insertRecords")
	public Object insertRecords(Counterpartfw counterpartfw) {
		counterpartfw.setEid(getCurrnetUserId());
		counterpartfw.setTime(new Date());
		if (iCounterpartfwService.insert(counterpartfw)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("001500100030")
	@GetMapping("/toUpdate/{id}")
	public Object toUpdate(@PathVariable("id") String id) {
		Map map = new HashMap();
		Counterpart counterpart = iCounterpartService.selectById(id);
		map.put("Counterpart", counterpart);
		Employee employee = iEmployeeService.selectById(counterpart.getEid());
		map.put("employee", employee);
		return responseSuccess(map);
	}

	@RequiresPermissions("001500100030")
	@PutMapping("/update")
	public Object update(Counterpart counterpart, String[] areaIds) {
		if (iCounterpartService.updateById(counterpart)) {
			return responseSuccess();
		} else {
			return responseFail();
		}

	}

	@RequiresPermissions("001500000005")
	@DeleteMapping("/delete/{id}")
	public Object delete(@PathVariable("id") String[] id) {
		if (iCounterpartService.deleteBatchIds(Arrays.asList(id))) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("00150010")
	@GetMapping("/getEmployeeName/{id}")
	public Object getEmployeeName(@PathVariable("id") String id) {
		School school = iSchoolService.selectById(id);
		Employee employee = iEmployeeService.selectById(school.getEid());
		return responseSuccess(employee);
	}

	@RequiresPermissions("001500100035")
	@GetMapping("/toAssignment/{id}")
	public Object toAssignment(@PathVariable("id") String id) {
		return responseSuccess(iCounterpartService.selectById(id));
	}

	/**
	 * 分配跟进人
	 * 
	 * @param id
	 * @return
	 */
	@RequiresPermissions("001500100035")
	@PutMapping("/assignment")
	public Object assignment(Counterpart counterpart) {
		if (iCounterpartService.updateById(counterpart)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

}
